import { fileURLToPath, URL } from "node:url";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite';
import Icons from "unplugin-icons/vite";
import IconsResolver from "unplugin-icons/resolver";
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import path from 'path';
// import dts from 'vite-plugin-dts';
// 导入模块配置
// import { moduleConfig } from './module.config';
import federation from '@originjs/vite-plugin-federation';

export default defineConfig({
  plugins: [
    vue(),
    federation({
      name: 'deviceModule',  // 修正为deviceModule，与平台端的remotes配置匹配
      filename: 'moduleEntry.js',
      remotes: {
        hostModule: 'http://localhost:3000/assets/hostEntry.js',
      },
      exposes: {
        './main': './src/index.ts'
      },
      shared: {
        vue: {
          import: false,  // 插件端不主动导入，复用平台
          shareScope: 'default',
          version: '^3.5.20'
        },
        pinia: {
          import: false,
          shareScope: 'default',
          version: '^2.3.1'
        },
        'element-plus': {
          import: false,
          shareScope: 'default',
          version: '^2.11.1'
        },
        '@element-plus/icons-vue': {
          import: false,
          shareScope: 'default'
        }
      }
    }),
    // dts({
    //   include: ['src/**/*.ts', 'src/**/*.d.ts', 'src/**/*.vue'],
    //   // 使用配置的输出目录
    //   outDir: moduleConfig.getOutputDirectory(),
    //   staticImport: true,
    //   insertTypesEntry: true,
    //   cleanVueFileName: true,
    //   copyDtsFiles: true,
    // }),
    createSvgIconsPlugin({
      iconDirs: [path.resolve(process.cwd(), "src/assets/svg")],
      symbolId: "i-[dir]-[name]"
    }),
    AutoImport({
      resolvers: [
        IconsResolver({ prefix: "Icon" }),
        ElementPlusResolver()
      ],
    }),
    Components({
      resolvers: [
        IconsResolver({ enabledCollections: ["ep"] }),
        ElementPlusResolver()
      ],
    }),
    Icons({
      autoInstall: true,
      compiler: "vue3"
    }),
  ],
  resolve: {
    alias: {
      "#/": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
  server: {
    port: 3004,
    cors: true,
  },
  build: {
    outDir: 'dist',
    emptyOutDir: true,
    target: ['es2022', 'edge89', 'firefox89', 'chrome89', 'safari15'],
    rollupOptions: {
      output: {
        assetFileNames: 'assets/[name][extname]',
        entryFileNames: 'assets/[name].js'
      }
    },
    minify: false,
    sourcemap: false,
  },
});